package com.fallrain.fallrainspringboot.controller;
import com.fallrain.fallrainspringboot.entity.User;
import com.fallrain.fallrainspringboot.entity.vo.UserVO;
import com.fallrain.fallrainspringboot.service.impl.UserServiceImpl;
import com.fallrain.fallrainspringboot.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* (user)表控制层
*
* @author fallrain
*/
@Api(tags="user表控制层")
@RestController
@RequestMapping("/user")
public class UserController {
    /**
    * 服务对象
    */
    @Resource
    private UserServiceImpl userServiceImpl;


    /**
     * delete by primary key
     * @param id
     * @return
     */
    @ApiOperation("通过id删除")
    @DeleteMapping("/deleteByPrimaryKey")
    public Result<?> deleteByPrimaryKey(Long id) {

        return Result.ok(userServiceImpl.deleteByPrimaryKey(id) > 0);

    }

    /**
     * insert record to table
     * @param record
     * @description: 新增 无字段为null判断
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/insert(无选择)")
    public Result<?> insert(@RequestBody User record) {

        return Result.ok(userServiceImpl.insert(record) > 0);

    }


    /**
     * insert record to table or update record
     * @param record
     * @description: 新增或者更新 无字段为null判断
     * @return
     */
    @ApiOperation("新增或者更新(无选择)")
    @PostMapping("/insertOrUpdate")
    public Result<?> insertOrUpdate(@RequestBody User record) {

        return Result.ok(userServiceImpl.insertOrUpdate(record) > 0);

    }

    /**
     * insert record to table or update record
     * @param record
     * @description: 新增或者更新 有字段为null判断
     * @return
     */
    @ApiOperation("新增或者更新(有选择)")
    @PostMapping("/insertOrUpdateSelective")
    public Result<?> insertOrUpdateSelective(@RequestBody User record) {

        return Result.ok(userServiceImpl.insertOrUpdateSelective(record) > 0);

    }

    /**
     * insert record to table selective
     * @param record
     * @description: 新增 有字段为null判断
     * @return
     */
    @ApiOperation("新增(有选择)")
    @PostMapping("/insertSelective")
    public Result<?> insertSelective(@RequestBody @Validated UserVO record) {
        User user = new User();
        BeanUtils.copyProperties(record, user);
        return Result.ok(userServiceImpl.insertSelective(user) > 0);

    }

    /**
    * select by primary key
    *
    * @param id 主键
    * @return 单条数据
    */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("/selectOne")
    public Result<?> selectOne(Long id) {

        return Result.ok(userServiceImpl.selectByPrimaryKey(id));

    }

    /**
     * update record selective
     *
     * @param record the updated record
     * @return update count
     */
    @ApiOperation("更新(有选择)")
    @PutMapping("/updateByPrimaryKeySelective")
    public Result<?> updateByPrimaryKeySelective(@RequestBody User record) {

        return Result.ok(userServiceImpl.updateByPrimaryKeySelective(record) > 0);

    }

    /**
     * update record
     *
     * @param record the updated record
     * @return update count
     */
    @ApiOperation("通过主键更新")
    @PutMapping("/updateByPrimaryKey")
    public Result<?> updateByPrimaryKey(@RequestBody User record) {

        return Result.ok(userServiceImpl.updateByPrimaryKey(record) > 0);

    }

    /**
     * record the batch updated record
     *
     * @param list the batch updated record
     * @return update count
     */
    @ApiOperation("通过主键批量更新")
    @PutMapping("/updateBatch(无选择)")
    public Result<?> updateByPrimaryKey(@RequestBody List<User> list) {

        return Result.ok(userServiceImpl.updateBatch(list) > 0);

    }

    /**
     * record the batch insert record
     *
     * @param list the batch insert record
     * @return update count
     */
    @ApiOperation("批量新增(无选择)")
    @PutMapping("/batchInsert")
    public Result<?> batchInsert(@RequestBody List<User> list) {

        return Result.ok(userServiceImpl.batchInsert(list) > 0);

    }

}
